﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System;
using Cinemachine;
using Main;

namespace XXYFramework
{
    /// <summary>
    /// 角色控制输入
    /// </summary>
	[DefaultExecutionOrder(-10)]
    public class PlayerInput : MonoBehaviour
    {
        PlayerUIControl uiControl;
        @CharacterInputMap inputMap;
        private void Awake()
        {
            //InputSystem.EnableDevice(Mouse.current);
            //InputSystem.EnableDevice(Keyboard.current);
            inputMap = new CharacterInputMap();

            if (Camera.main)
            {
                if (!Camera.main.TryGetComponent<CinemachineBrain>(out CinemachineBrain brain))
                {
                    brain = Camera.main.gameObject.AddComponent<CinemachineBrain>();
                    brain.m_DefaultBlend = new CinemachineBlendDefinition(0, 0);
                }
            }
            else
            {
                Debug.LogError("没有找到 Main Camera");
            }
        }
        private void OnEnable()
        {
            inputMap.Enable();
        }
        private void Start()
        {
            inputMap.InputMap.Jump.performed += Jump_performed;
            inputMap.InputMap.Roll.performed += Roll_performed;
            inputMap.InputMap.ViewToggle.performed += ViewToggle_performed;
            // 蹲伏
            inputMap.InputMap.Crouch.started += Crouch_started;
            inputMap.InputMap.Crouch.canceled += Crouch_canceled;
            // 奔跑
            inputMap.InputMap.Sprint.started += Sprint_started;
            inputMap.InputMap.Sprint.canceled += Sprint_canceled;

            uiControl = GetComponentInChildren<PlayerUIControl>(true);
            uiControl.ECrouchStart += ECrouchStart;
            uiControl.ECrouchEnd += ECrouchEnd;
            uiControl.ESprintStart += ESprintStart;
            uiControl.ESprintEnd += ESprintEnd;
            uiControl.EJump += EJump;
            uiControl.ERoll += ERoll;
            uiControl.EViewToggle += EViewToggle;

#if UNITY_IOS || UNITY_ANDROID
            uiControl.gameObject.SetActive(true);
#elif UNITY_WEBGL && !UNITY_EDITOR
            uiControl.gameObject.SetActive(JSLibrary.IsMobile());
#else
            uiControl.gameObject.SetActive(false);
#endif
        }

        private void ViewToggle_performed(InputAction.CallbackContext obj)
        {
            EViewToggle?.Invoke();
        }

        private void Sprint_canceled(InputAction.CallbackContext obj)
        {
            ESprintEnd?.Invoke();
        }

        private void Sprint_started(InputAction.CallbackContext obj)
        {
            ESprintStart?.Invoke();
        }

        private void Crouch_started(InputAction.CallbackContext obj)
        {
            //Debug.Log("开始蹲伏");
            ECrouchStart?.Invoke();
        }

        private void Crouch_canceled(InputAction.CallbackContext obj)
        {
            //Debug.Log("结束蹲伏");
            ECrouchEnd?.Invoke();
        }

        private void Jump_performed(InputAction.CallbackContext obj)
        {
            //Debug.Log("跳跃");
            EJump?.Invoke();
        }

        private void Roll_performed(InputAction.CallbackContext obj)
        {
            //Debug.Log("翻转");
            ERoll?.Invoke();
        }

        #region 事件
        /// <summary>
        /// 蹲伏
        /// </summary>
        public event Action ECrouchStart;
        public event Action ECrouchEnd;
        /// <summary>
        /// 奔跑
        /// </summary>
        public event Action ESprintStart;
        public event Action ESprintEnd;
        /// <summary>
        /// 跳跃
        /// </summary>
        public event Action EJump;
        /// <summary>
        /// 翻转
        /// </summary>
        public event Action ERoll;
        /// <summary>
        /// 人称切换
        /// </summary>
        public event Action EViewToggle;

        #endregion
        #region 属性
        /// <summary>
        /// 获取 移动数值
        /// </summary>
        public Vector2 GetMoveValue
        {
            get
            {
                if (uiControl.isActiveAndEnabled)
                {
                    return uiControl.GetMoveValue;
                }
                else
                {
                    return inputMap.InputMap.Move.ReadValue<Vector2>();
                }
            }
        }
        /// <summary>
        /// 获取 视口移动向量
        /// </summary>
        public Vector2 GetLookValue
        {
            get
            {
                if (uiControl.isActiveAndEnabled)
                {
                    return uiControl.GetLookValue;
                }
                else
                {
                    return inputMap.InputMap.Look.ReadValue<Vector2>();
                }
            }
        }
        /// <summary>
        /// 获取鼠标滚轮数值
        /// </summary>
        public float GetZoomValue
        {
            get
            {
                if (uiControl.isActiveAndEnabled)
                {
                    return uiControl.GetZoomValue;
                }
                else
                {

                    return inputMap.InputMap.Zoom.ReadValue<float>() * 0.001f;
                }
            }
        }
        #endregion
        private void OnDisable()
        {
            inputMap.Disable();
        }
        private void OnDestroy()
        {
            inputMap.Dispose();
        }
    }
}